#include "register.h"
#include "ui_register.h"
#include <QMessageBox>
#include <QPushButton>
#include "login.h"
#include <QFile>
#include <QTextStream>
Register::Register(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Register)
{
    ui->setupUi(this);
    connect(ui->pushButton_Register,&QPushButton::clicked,this,&Register::mRegister);
    connect(ui->pushButton,&QPushButton::clicked,this,&Register::pushButton_clicked);
    this->setWindowTitle("注册界面");
}

Register::~Register()
{
    delete ui;
}

void Register::mRegister()
{
    QString username = ui->lineEdit_Username->text();
    QString password = ui->lineEdit_Password->text();
    QString confirmPassword = ui->lineEdit_ConfirmPassword->text();

    //注册成功返回登录界面
    if(password == confirmPassword)
    {
        //储存用户和密码

        QMessageBox::information(this,"成功","注册成功");
        Login *loginWindow = new Login(this);
        loginWindow->show();

        this->hide();
    }
    else
    {
        QMessageBox::warning(this,"错误","两次输入的密码不一致");
    }

    QFile file("user.txt");

    if(file.open(QIODevice::Append | QIODevice:: Text))
    {
        QTextStream out(&file);
        out << username << "," << password <<"\n";
        file.close();
        //this->close();
    }
}

void Register::pushButton_clicked()
{
    Login *loginWindow = new Login(this);
    loginWindow->show();
    this->hide();
}

